/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.maixiami;

import com.taobao.api.ApiException;
import com.taobao.api.domain.ItemCat;
import com.taobao.api.domain.TaobaokeItem;
import com.taobao.api.request.ItemsGetRequest;
import com.taobao.api.response.ItemsGetResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.log4j.Logger;

/**
 *
 * @author qye
 */
public class ItemCategoriesStatistics {
    static Logger logger = Logger.getLogger(ItemCategoriesStatistics.class);
    
     
    ItemCategories<ItemCat> itemCategories;
    List<ItemCat> nodes;
    public ItemCategoriesStatistics(List<ItemCat> itemCats) {
        itemCategories = new ItemCategories<ItemCat>(itemCats);
    }

    public ItemCategoriesStatistics(String catsfile) throws FileNotFoundException, IOException, ClassNotFoundException {
        nodes = (List<ItemCat>)Utils.readObjectFromFile(catsfile);
        
        itemCategories = new ItemCategories<ItemCat>(nodes);
    }
    
    public void calculate() throws InterruptedException {
        TreeMap<Long, Long> cidCountMap = calculate(0L);
        Object[] keys = cidCountMap.entrySet().toArray();
        for(int i = cidCountMap.size() - 12; i < cidCountMap.size(); i++) {
            System.out.println("\n");
            calculate(((Map.Entry<Long, Long>)keys[i]).getValue());
        }
    }
    
    public TreeMap<Long, Long> calculate(Long parentid) throws InterruptedException {
        ItemsGetRequest req = new ItemsGetRequest();
        req.setFields("num_iid,volume,cid");
        req.setPageSize(1L);
        TreeMap<Long, Long> cidCountMap = new TreeMap<Long, Long>();
        
        for(ItemCat itemCat : itemCategories.getChildren(parentid)) {
            req.setCid(itemCat.getCid());
            try {
                ItemsGetResponse res = App.getTaobaoClient().execute(req);
                if(res.isSuccess()) {
                    cidCountMap.put(res.getTotalResults(), itemCat.getCid());
                    System.out.println("" + itemCat.getCid() + "\t" + itemCat.getName() + "\t" + res.getTotalResults());
                } else {
                    // retry once
                    res = App.getTaobaoClient().execute(req);
                    if(res.isSuccess()) {
                        cidCountMap.put(res.getTotalResults(), itemCat.getCid());
                        System.out.println("" + itemCat.getCid() + "\t" + itemCat.getName() + "\t" + res.getTotalResults());
                    } else {
                        logger.warn(res.getMsg());
                        logger.warn(itemCat.getCid());
                    }
                }
                Thread.sleep(100);
            } catch (ApiException ex) {
                logger.warn(ex);
                logger.warn(itemCat.getCid());
            }
        } // end of for
        return cidCountMap;
    }
}
